<template>
	<view>
		<u-navbar :title="$t('确认订单')"></u-navbar>
		<view class="wrap">
			<view class="address" @click="$.to('/pagesA/pages/my/address?state=1')">
				<view class="left">
					<image src="../../../static/image/a1_.png" mode=""></image>
					<view v-if="!isAdd">{{ $t("添加收货地址") }}</view>
					<view class="info" v-else>
						<view class="name">
							{{address.fullname}} <text>{{address.mobile}}</text>
						</view>
						<view class="add">
							{{address.province + ' ' + address.city + ' ' + address.area + ' ' + address.detailed}}
						</view>
					</view>
				</view>
				<u-icon name="arrow-right" color="#ccc"></u-icon>
			</view>

			<view class="goodsBox">
				<view class="goods" :key="idx">
					<image :src="goods_info.image" mode=""></image>
					<view class="goodsInfo">
						<view class="tit">{{goods_info.name}}</view>

						<view class="price n-flex-row n-justify-between" style="margin-top: 10rpx;">
							<view>￥0</view>
							<view style="color: #999;font-size: 28rpx;">x{{goods_info.goods_num}}</view>
						</view>
						<view style="color: #999;font-size: 28rpx;" v-if="goods_info.goods_type==2">自动发货</view>
					</view>
				</view>
				<view class="row">
					<view>订单类型</view>
					<view>提货订单</view>
				</view>

				<view class="row">
					<view>运费</view>
					<view class="pri" v-if="yunfei == 0">免运费</view>
					<view class="pri" v-else>￥{{yunfei}}</view>
				</view>
				<view class="beizhu">
					<view class="">备注信息</view>
					<u-input v-model="remarks" type="textarea" height="50" placeholder="选填备注信息"/>
				</view>
			</view>
			
			<view class="box1" style="margin-top: 20rpx;" v-if="yunfei > 0">
				<u-radio-group style="display: block;" width="34rpx" v-model="paymode" active-color="#70DDE0">
					<!-- #ifdef MP-WEIXIN || H5 -->
					<view class="rowitem payrow" @click="selectPay('wechat')">
						<view class="l">
							<image src="../../../static/image/weixinf.png" mode=""></image>
							微信
						</view>
						<u-radio name="wechat"></u-radio>
					</view>
					<!-- #endif -->
					<!-- #ifdef APP-PLUS || H5 -->
					<view class="rowitem payrow" @click="selectPay('alipay')">
						<view class="l">
							<image src="../../../static/image/zhifubao.png" mode=""></image>
							支付宝
						</view>
						<u-radio name="alipay"></u-radio>
					</view>
					<!-- #endif -->
				</u-radio-group>
			</view>
		</view>
		<view class="rule">
			<rich-text :nodes="rule"></rich-text>
		</view>
		<view style="height: 200rpx;">
			
		</view>
		
		<view class="btm">
			<view class="btmbox">
				<view class="n-flex-row n-align-center">
					<view class="">
						<view>共{{goods_num}}件 | 合计：￥<text>{{yunfei}}</text></view>
						<view class="yun" v-if="yunfei != 0">(含运费￥{{yunfei}})</view>
						<!-- <view class="yun" v-if="yunfei == 0">单次提货满3件及以上免费包邮</view> -->
					</view>
					
					<button @click="pay">提交订单</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				remarks: '',
				address: '',
				isAdd: false, //是否有地址
				yunfei:0,
				ids: '',
				goods_num:0,
				goods_info:{},
				// #ifdef MP-WEIXIN || H5
				paymode:'wechat',
				// #endif
				
				// #ifdef APP-PLUS
				paymode:'alipay',
				// #endif
				rule:'',
			}
		},
		onLoad(option) {
			this.goods_id = option.goods_id;
			this.goods_num = option.goods_num;
			this.getGoodsData();
			this.getAddress();
			uni.$on('changeAddress', (addId) => {
				console.log(addId, 'id');
				this.upDataAddress(addId);
			})
			this.get_apply_deliver_price();
			this.getRule()
		},
		onUnload() {
			uni.removeStorageSync('tihuo')
			uni.$off('changeAddress');
		},
		methods: {
			getGoodsData(){
				this.$http({
					url: "api/common/get_goodsdata",
					data: {
						'goods_id': this.goods_id,
						'goods_num': this.goods_num
					}
				}).then(res => {
					console.log("33333");
					this.goods_info = res.data.data.goods_info;
					this.goods_num = res.data.data.goods_info.goods_num;
					console.log(res);
	
				}).catch(err => {});
			},
			//获取是否有额外费用
			get_apply_deliver_price(){
				this.$http({
					url: "api/order/get_apply_deliver_price",
					data: {
						'goods_id': this.goods_id,
						'goods_num': this.goods_num
					}
				}).then(res => {
					console.log(res);
					if (res.data.code == 1) {
						this.yunfei = res.data.data;
					}
				}).catch(err => {});
			},
			getRule(){
				this.$http({
					url: "api/common/get_agreement",
					data: {
						name: 'delivery_rule'
					}
				}).then(res => {
					if (res.data.code == 1) {
						this.rule = res.data.data.content
					} else {
						this.$tip.tip(res.data.msg)
					}
				}).catch(err => {});
			},
			pay() {
				if (!this.address) {
					this.$tip.tip("请选择地址")
					return
				}
				if(this.yunfei > 0){
					if (this.paymode == "") {
						this.$tip.tip("请选择支付方式")
						return
					}
				}
				this.$http({
					url: "api/order/apply_deliver",
					data: {
						goods_id: this.goods_id,
						goods_num: this.goods_num,
						address_id: this.address.id,
						paymode:this.paymode,
						remarks: this.remarks,
						client: 1,
					}
				}).then(res => {
					if(res.data.code==1){
						// #ifdef H5
						this.fun.go_h5pay(res.data.data);
						// #endif
						
						// #ifdef MP-WEIXIN
						this.fun.go_mpwxpay(res.data.data).then(payres => {
							this.$tip.tip("支付成功", "success")
							setTimeout(() => {
								uni.redirectTo({
									url: '/pagesB/pages/mall/paySuccexx'
								})
							}, 700)
						}).catch(payerr => {
							this.$tip.tip('支付失败')
						});
						// #endif
						
						// #ifdef APP-PLUS
						if(this.paymode=='wechat'){
							this.fun.go_appwxpay(res.data.data.pay_data).then(payres => {
								this.$tip.tip("支付成功", "success") 
								setTimeout(() => {
									uni.redirectTo({
										url: '/pagesB/pages/mall/paySuccexx'
									})
								}, 700)
							}).catch(payerr => {
								this.$tip.tip('支付失败')
							});
						}else{
							this.fun.go_appalipay(res.data.data.pay_data).then(payres => {
								this.$tip.tip("支付成功", "success") 
								setTimeout(() => {
									uni.redirectTo({
										url: '/pagesB/pages/mall/paySuccexx'
									})
								}, 700)
							}).catch(payerr => {
								this.$tip.tip('支付失败')
							});
						}
						// #endif
					} else if (res.data.code == 8) {
						this.$tip.tip(res.data.msg, "success")
						setTimeout(() => {
							uni.redirectTo({
								url: '/pagesB/pages/mall/paySuccexx'
							})
						}, 700)
						return
					} else {
						this.$tip.tip(res.data.msg)
					}
				}).catch(err => {});
				
			},
			getAddress() {
				this.$http({
					url: "api/member/my_address",
				}).then(res => {
					console.log(res);
					if (res.data.code == 1) {
						if (res.data.data.length == 0) {
							this.isAdd = false
						} else {
							this.isAdd = true
						}
						this.address = res.data.data[0]
						console.log(this.address);
					} else {
						this.$tip.tip(res.data.msg)
						this.isAdd = false
					}
				}).catch(err => {});
			},
			// 更新收货地址
			upDataAddress(id) {
				this.$http({
					url: "api/member/get_address",
					data: {
						address_id: id
					}
				}).then(res => {
					console.log(res, "dizhi");
					if (res.data.code == 1) {
						this.address = res.data.data
						this.isAdd = true
					}

				}).catch(err => {});
			},
			// 选择支付方式
			selectPay(mode) {
				this.paymode = mode
			},
		}
	}
</script>

<style>
	@import url("../../../static/css/confirmOrder.css");
</style>
